//
//  NXFlowLayout.m
//  项目A --半糖
//
//  Created by romana on 16/6/3.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "NXFlowLayout.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface NXFlowLayout ()

//用来存每一列的y值
@property(nonatomic,strong)NSMutableDictionary *columYDic;

//存放item的attribute对象
@property(nonatomic,strong)NSMutableArray *attributesArray;

@end


@implementation NXFlowLayout


#pragma mark--第三个方法
-(instancetype)init
{
    self = [super init];
    if (self) {
        
        //        self.columCounts = 3;//3列
        //        self.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);//边界距离
        self.columYDic = [NSMutableDictionary dictionary];
        //        self.columSpace = 10;//列间距
        //        self.rowSpace = 10;//行间距
        
        self.attributesArray = [NSMutableArray array];
        
    }
    
    return self;
}
#pragma mark--第一个方法
//重写系统prepareLayout方法,当collectionview布局item的时候,该方法会被执行
-(void)prepareLayout
{
    [super prepareLayout];
    
    
    
    //2.获取collectionview上item的个数
    self.itemCounts = [self.collectionView numberOfItemsInSection:0];
    
    // 总页数
    self.totalPage = self.itemCounts / (self.colum * self.row);
    
    if (self.itemCounts % (self.colum * self.row) != 0) {
        
        self.totalPage += 1;
    }
    
    //设置每个item的frame
    for (int i = 0; i < self.itemCounts; i++) {
        
        [self setItemFrame:i];
        
    }
    
    
}

#pragma mark--第二个方法
//自定义一个方法设置每个item的信息
-(void)setItemFrame:(NSInteger)index
{
    int onePageNum = (int)(self.row * self.colum);
    self.currentPage = index / (self.row*self.colum); // 超过4个为1页
    //  index = 4 % 4 % 2 = 0
    rowNum = (int)(index % onePageNum) % self.colum;
    // 4 % 4 / 2 = 0;
    columnNum = (int)(index % onePageNum) / self.colum;
    
    // 宽 = 屏幕宽-左边间距-右边间距-(列数-1)*列间距
    CGFloat itemWidth = (WIDTH - self.edgeInsets.left - self.edgeInsets.right - (self.colum - 1)*self.columSpace)/self.colum;
    CGFloat itemHeight = [self.ytDelegate NXFlowLayout:self heightForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] width:itemWidth];
    
    // 列和行的下标都是0-1的范围
    //x = 左间距 + 当前页码*屏幕宽度 + item所在当前页行下标*item的宽+item所在当前页行的下标*行间距
    CGFloat x = self.edgeInsets.left + self.currentPage * WIDTH + rowNum * itemWidth + rowNum * self.rowSpace;
    // y = 上边界 + 当前item所在列的下标*item的高度 + item所在列的下标*行间距
    CGFloat y = self.edgeInsets.top + columnNum * itemHeight + columnNum * self.rowSpace;
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.frame = CGRectMake(x, y, itemWidth, itemHeight);
    
    [self.attributesArray addObject:attributes];
    
}
#pragma mark--第四个方法  先走完prepareLayout方法再走这个方法
//告诉layout去布局数组中的item信息
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attributesArray;
}


#pragma mark--第5个方法
//重新设置滚动范围
-(CGSize)collectionViewContentSize
{
    return CGSizeMake(self.totalPage*WIDTH, 0);
}




@end
